package com.topcycle.tlight;

import android.content.Context;
import android.util.Log;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by Administrator on 15-7-21.
 */
public class Storage {
    private static final String FILENAME = "mydev.dat";

    public Storage() {

    }

    static class TestClass implements Serializable {
        int index;
        String name;

        TestClass(int index,String name) {
            this.index = index;
            this.name = name;
        }

        public TestClass() {

        }
    }

    public static void testSave(Context ctx) throws Exception{
        TestClass testClass1 = new TestClass(1,"aaaa");
        TestClass testClass2 = new TestClass(2,"bbbb");
        TestClass testClass3 = new TestClass(3,"cccc");
        ArrayList<TestClass> testlist = new ArrayList<TestClass>();
        testlist.add(testClass1);
        testlist.add(testClass2);
        testlist.add(testClass3);
        try {
            FileOutputStream outputStream = ctx.openFileOutput(FILENAME,ctx.MODE_PRIVATE);
            ObjectOutputStream obj = new ObjectOutputStream(outputStream);
            obj.writeObject(testlist);
            obj.close();
            FileInputStream inputStream = ctx.openFileInput(FILENAME);
            ObjectInputStream iobj = new ObjectInputStream(inputStream);
            ArrayList<TestClass> outlist = new ArrayList<TestClass>();
            outlist =(ArrayList<TestClass>) iobj.readObject();
            TestClass testClass = new TestClass();
            testClass = outlist.get(0);
            Log.e("test",""+testClass.name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void saveData(ArrayList<Device> devlist,Context ctx){
        try {
            FileOutputStream outputStream = ctx.openFileOutput(FILENAME, ctx.MODE_PRIVATE);
            ObjectOutputStream obj = new ObjectOutputStream(outputStream);
            obj.writeObject(devlist);
            obj.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static ArrayList<Device> readData(Context ctx) {
        ArrayList<Device> mList = new ArrayList<Device>();
        try {
            FileInputStream inputStream = ctx.openFileInput(FILENAME);
            ObjectInputStream obj = new ObjectInputStream(inputStream);
            mList = (ArrayList<Device>) obj.readObject();
            obj.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return mList;
    }

}
